const fastify = require('fastify')
const cors = require('@fastify/cors')
const mongo = require('@fastify/mongodb')
const {ObjectId} = require('@fastify/mongodb')
// const mongoose = require('mongoose')
const app = fastify()





// 配置跨域 register 注册插件
app.register(cors)
// 配置数据库
app.register(mongo, {
    // localhost 就是 127.0.0.1 host 文件
    // /test 数据库的名字
    url: 'mongodb://127.0.0.1:27017/test',
    // 每次前端请求完，都关闭 mongo 的连接
    forceClose: true
})


// 添加用户信息
app.put('/userinfo', async (req, res) => {
    // 1. 拿到 db 对象
    const db = app.mongo.db
    const userinfo = db.collection('userinfo')
    const rest = await userinfo.insertOne({
        "id": req.body.id,
        "name": req.body.name,
        "nickname": req.body.nickname,
        "phone": req.body.phone,
        "email": req.body.email,
        "character": req.body.character,
        "dept": req.body.dept,
        "registTime": req.body.registTime,
        "password": req.body.password,
    })
    return rest
    // 3. 增加数据{}
    // await teachers.insertOne({
    //     name: "杜帅",
    //     desc: "帅的一批"
    // })

    // 5. 删除数据
    // await teachers.deleteOne({
    //     name: "杜帅"
    // })

    // 6. 修改数据
    // await teachers.updateOne({
    //     name: "杜帅"
    //     // $set 修改的数据
    // }, {
    //     $set: {
    //         desc: "全黑马颜值巅峰"
    //     }
    // })

    // 4. 查询数据
    // toArray 查到的数据转换成数组
    // const data = await userinfo.find({}).toArray()

    // console.log(data)
    return rest
})
// 获取所有用户信息

// 获取具体用户信息
app.get('/userinfo/:id', async (req, res) => {
    const db = app.mongo.db
    const userinfo = db.collection('userinfo')
    console.log(req.params);
    const result = await userinfo.findOne({ _id: new ObjectId(req.params.id) });
    return result;
});

// 修改用户信息
app.put('/userinfo/:id', async (req, res) => {
    const db = app.mongo.db;
    const userinfo = db.collection('userinfo');
    const result = await userinfo.updateOne(
      { _id: new ObjectId(req.params.id) },
      {
        $set: {
          name: req.body.name,
          nickname: req.body.nickname,
          phone: req.body.phone,
          email: req.body.email,
          character: req.body.character,
          dept: req.body.dept,
          registTime: req.body.registTime,

        }
      }
    );
    return result;
  });

// 修改用户密码
app.put('/userinfo/:id/password', async (req, res) => {
    const db = app.mongo.db;
    const userinfo = db.collection('userinfo');
    const result = await userinfo.updateOne(
      { _id: new ObjectId(req.params.id) },
      { $set: { password: req.body.password } }
    );
    return result;
  });

// 添加组织架构数据
  app.put('/origin', async (req, res) => {
    // 1. 拿到 db 对象
    const db = app.mongo.db
    const org= db.collection('org')
    const rest = await org.insertOne({
        // "id": req.body.id,
        "origin": req.body.origin,
        "class": req.body.class,
        "phone": req.body.phone,
        "header": req.body.header,
        "number": req.body.number,
        "status": req.body.status,
    })
    return rest
    })

    // 获取所有成员信息
    app.get('/origin', async (req, res) => {
      // 1. 拿到 db 对象
      const db = app.mongo.db
      const org= db.collection('org')
      const rest = await org.find({}).toArray()
      return rest
      })

      // 修改组织架构数据
      app.put('/origin/:id', async (req, res) => {
        const db = app.mongo.db;
        const org = db.collection('org');
        const result = await org.updateOne(
          { _id: new ObjectId(req.params.id) },
          {
            $set: {
              origin: req.body.origin,
              class: req.body.class,
              phone: req.body.phone,
              header: req.body.header,
              number: req.body.number,
              status: req.body.status,    
            }
          }
        )})

        // 删除组织架构数据
        app.delete('/origin/:id', async (req, res) => {
          const db = app.mongo.db;
          const org = db.collection('org');
          const result = await org.deleteOne({ _id: new ObjectId(req.params.id) });
          return result;
        });

        // 
app.listen({
    port: 3000,
    host: '0.0.0.0'
}).then((addr) => {
    // 服务器启动的回调
    console.log(`服务器启动在 ${addr}`)
})

